import httpUtil from "@/utils/httpUtil";
import deviceUtil from "../utils/deviceUtil";
import messageUtil from "../utils/messageUtil";
import logApi from "./logApi";


const login = (mobile, captcha, callBack) => {
	let a = httpUtil.getA()
	let user_id = getUserId()
	let type = 1; //类型:1=支付宝小程序,2=微信h5,3=微信小程序,4=支付宝h5，默认1
	// #ifdef H5
	if (deviceUtil.isAliBrowser()) {
		type = 4
	}
	if (deviceUtil.isWeChatBrowser()) {
		type = 2
	}
	// #endif
	// #ifdef MP-ALIPAY
	type = 1;
	// #endif
	// #ifdef MP-WEIXIN
	type = 3
	// #endif
	let params = {
		a,
		mobile,
		captcha,
		user_id,
		type,
		event: 'mobilelogin',
	}
	httpUtil.post('/api/rf/login', params, res => {
		setToken(res.data.token)
		setUserInfo(res.data.user)
		if (callBack) {
			callBack(res)
		}
	})
}

const setToken = (token) => {
	uni.setStorageSync('token', token)
}

const getToken = () => {
	return uni.getStorageSync('token')
}

const setUserInfo = (userInfo) => {
	uni.setStorageSync('userInfo', JSON.stringify(userInfo))
}

const getUserInfo = () => {
	let userInfo = uni.getStorageSync('userInfo')
	if (userInfo) {
		return JSON.parse(userInfo)
	}
	return false
}

const initUserInfo = (callBack) => {
	let token = getToken()
	let params = {
		token
	}
	httpUtil.post('/api/rf/getUserInfo', params, res => {
		setUserInfo(res.data.user)
		if (callBack) {
			callBack(res)
		}
	})
}

/**
 *
 * @param type 爱奇艺，腾讯视频，芒果TV，优酷会员，南瓜视频
 * @param img
 * @param callBack
 */
const receive = (type, img, callBack) => {
	let params = {
		type,
		img
	}
	httpUtil.post('/api/rf/lqvip', params, res => {
		if (callBack) {
			callBack(res)
		}
	})
}

/**
 * 领取记录列表
 * @param callBack
 */
const receiveRecordList = (callBack) => {
	httpUtil.post('/api/rf/lqlog', {}, res => {
		if (callBack) {
			callBack(res)
		}
	})
}

const setUserId = (userId) => {
	uni.setStorageSync('userId', userId)
}

const getUserId = () => {
	return uni.getStorageSync('userId')
}


const initUserId = (code, callBack, errCallBack) => {
	// 类型:1=支付宝小程序,2=微信h5,3=微信小程序,4=支付宝h5，默认1
	let type = 0;
	// #ifdef H5
	if (deviceUtil.isWeChatBrowser()) {
		type = 2
	}
	if (deviceUtil.isAliBrowser()) {
		type = 4
	}
	// #endif
	// #ifdef MP-ALIPAY
	type = 1
	// #endif
	// #ifdef MP-WEIXIN
	type = 3
	// #endif
	let params = {
		code,
		type
	}
	httpUtil.post('/api/rf/getAliUseridHjk', params, res => {
		console.log('initUserId', res)
		setUserId(res.data.user_id)
		logApi.addLog('code换取user_id,请求参数：' + JSON.stringify(params) + '。返回值：' + JSON.stringify(res))
		if (callBack) {
			callBack(res.data)
		}
	}, err => {
		if (errCallBack) {
			errCallBack(err)
		}
	})
}
const getMiniLoginCode = (callBack, errCallBack) => {
	uni.getProvider({
		service: 'payment',
		success: res => {
			console.log('getProvider success', res);
			console.log(res.provider[0]);
			uni.login({
				provider: res.provider[0],
				success: loginRes => {
					// console.log("loginRes.code", loginRes.code);
					// getUserIdByServe(loginRes.code);
					if (callBack) {
						callBack(loginRes.code)
					}
				},
				fail: function(loginFail) {
					console.log("loginFail", loginFail);
				}
			});
		},
		fail: res => {
			console.log('getProvider fail', res);
			if (errCallBack) {
				errCallBack(res)
			}
		}
	})
}


/**
 * 将用户跳转到微信公共平台获取用户信息
 */
const gotoH5WxPlatformLoginUrl = () => {
	let pages = getCurrentPages();
	let currentPage = pages[pages.length - 1]['$page']['fullPath'];
	let url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' + httpUtil.wxCommonPlatformAppId +
		'&redirect_uri=' + encodeURIComponent(
			window.location.href) +
		'&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect';
	console.log('gotoH5WxPlatformLoginUrl', url)

	window.location.href = url;
}

const setParam = (param) => {
	uni.setStorageSync('param', param)
}

const getParam = () => {
	return uni.getStorageSync('param');
}

/**
 *  订单类型，上线之后新版本必传，订单类型:1=通用,2=速停车,3=芝麻GO,5=快手直播,6=P云PP停车
 */
const setOrderType = (ordertype) => {
	uni.setStorageSync('ordertype', ordertype)
}

const getOrderType = () => {
	return uni.getStorageSync('ordertype');
}

/**
 *  购买类型:1=季卡,2=半年卡,3=年卡，默认3
 */
const setBuyType = (buyType) => {
	uni.setStorageSync('buyType', buyType)
}

const getBuyType = () => {
	let buyType = uni.getStorageSync('buyType');
	if (!buyType) {
		return 3;
	}
	return buyType;
}

export default {
	login,
	getToken,
	initUserInfo,
	setUserInfo,
	getUserInfo,
	receive,
	receiveRecordList,
	setUserId,
	getUserId,
	getMiniLoginCode,
	initUserId,
	gotoH5WxPlatformLoginUrl,
	setParam,
	getParam,
	setOrderType,
	getOrderType,
	setBuyType,
	getBuyType
}